<?php
namespace App\Http\Controllers\Company;

use App\Exceptions\ApiException;
use App\Http\Requests\BasicRequest;
use App\Mail\Basic;
use App\Models\ClientUserRelationship;
use App\Models\HouseDistrict;
use App\Models\ServiceTeam;
use App\Models\ServiceTeamDetail;

class DepartmentController extends CommonController {


    public function team_list(BasicRequest $request){
        $service_num = ClientUserRelationship::Join('client_user_roles','client_user_roles.ship_id','=','client_user_relationship.id')
            ->where('client_user_relationship.village_id',$this->ship->village_id)->count();
        $service = collect(['0'=>['id'=>0,'name'=>'物业部','num'=>$service_num]]);
        $team = ServiceTeam::leftJoin('house_district','house_district.id','=','service_team.partition_id')
            ->select('service_team.id','service_team.name')
            ->where('house_district.parent_id','=',$this->ship->village_id)
            ->get()->each(function($v,$k){
                $v->num = ServiceTeamDetail::where(['team_id'=>$v->id])->count();
            });
        $service = $service->merge($team);
        return json_success('OK',$service);
    }

    public function team_detail(BasicRequest $request){
        $this->validate($request,[
            'id'=>'required',
        ],[
            'id.required'=>'缺少ID参数'
        ]);
        $id = $request->input('id');
        if($id==0){
            $list = ClientUserRelationship::leftJoin('client_user_roles','client_user_roles.ship_id','=','client_user_relationship.id')
                ->Join('client_user','client_user.id','=','client_user_roles.uid')
                ->where('client_user_relationship.village_id',$this->ship->village_id)
                ->select('client_user_relationship.name','client_user.mobile','client_user.avatar','client_user_roles.uid')
                ->pages();
        }else{
            $list = ServiceTeamDetail::Join('client_user','client_user.id','=','client_team_detail.uid')
                ->where('team_id',$id)
                ->select('service_team_detail.real_name','client_user.id','client_user.mobile','client_user.avatar')
                ->pages();
        }
        return json_success('OK',$list);
    }

    public function team_user(BasicRequest $request){
        $this->validate($request,[
            'ids'=>'required',
        ],[
            'ids.required'=>'缺少ID参数'
        ]);
        $ids = $request->input('ids');
        $id_array = explode(',',$ids);
        $return = collect([]);
        foreach($id_array as $key=>$val){
            if($val==0){
                $list = ClientUserRelationship::leftJoin('client_user_roles','client_user_roles.ship_id','=','client_user_relationship.id')
                    ->Join('client_user','client_user.id','=','client_user_roles.uid')
                    ->where('client_user_relationship.village_id',$this->ship->village_id)
                    ->select('client_user_relationship.name','client_user_roles.uid')
                    ->get();
            }else{
                $list = ServiceTeamDetail::Join('client_user','client_user.id','=','client_team_detail.uid')
                    ->where('team_id',$val)
                    ->select('service_team_detail.real_name','client_user.avatar')
                    ->get();
            }
            $return = $return->merge($list);
        }
        return json_success('OK',$return);
    }
}